package com.clas.liu.myapplication.model.impl;

import com.clas.liu.myapplication.model.entity.banner.BannerRoot;
import com.clas.liu.myapplication.model.entity.homeHeads.HomeHeadsRoot;
import com.clas.liu.myapplication.model.event.HomeFragmentEvent;
import com.clas.liu.myapplication.model.i.IHomeFragmentModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/2/28.
 */

public class HomeFragmentModel extends BaseFragmentModel<HomeFragmentEvent> implements IHomeFragmentModel{

    public HomeFragmentModel(OnEventReceiveListener<HomeFragmentEvent> listener) {
        super(listener);
    }

    @Override
    public void getBanner() {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                HomeFragmentEvent event = new HomeFragmentEvent();
                event.setWhat(HomeFragmentEvent.GET_BANNER_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    BannerRoot root = gson.fromJson(response.body().string(), BannerRoot.class);  //json解析
                    HomeFragmentEvent event = new HomeFragmentEvent();                        //事件
                    event.setWhat(HomeFragmentEvent.GET_BANNER_OK);                         //事件的设置
                    event.setBannerRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getBanner(callback);
    }

    @Override
    public void getHeads() {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                HomeFragmentEvent event = new HomeFragmentEvent();
                event.setWhat(HomeFragmentEvent.GET_HEADS_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    Gson gson = new Gson();
                    HomeHeadsRoot root = gson.fromJson(response.body().string(), HomeHeadsRoot.class);  //json解析
                    HomeFragmentEvent event = new HomeFragmentEvent();                        //事件
                    event.setWhat(HomeFragmentEvent.GET_HEADS_OK);                         //事件的设置
                    event.setHeadsRoot(root);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getheads(callback);
    }

    @Override
    public void getNoticeCount() {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                HomeFragmentEvent event = new HomeFragmentEvent();
                event.setWhat(HomeFragmentEvent.GET_NOTICE_COUNT_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()) {
                    int count = Integer.parseInt(response.body().string());
                    HomeFragmentEvent event = new HomeFragmentEvent();                        //事件
                    event.setWhat(HomeFragmentEvent.GET_NOTICE_COUNT_OK);                         //事件的设置
                    event.setNoticeCount(count);
                    postEvent(event);
                }else
                    onFailure(call, new IOException(response.body().string()));
            }
        };
        interfaces.getNoticeCount(callback);
    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
